package com.nativetalk.service.four.comment;

import com.nativetalk.base.Page;
import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentInsert;
import com.nativetalk.bean.comment.TdNtCommentList;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.dao.TdNtCommentDao;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 2016/10/31.
 */
@Service
public class FourCommentServiceImpl implements FourCommentService {
    private static Logger logger = Logger.getLogger(FourCommentServiceImpl.class);
    @Resource
    private TdNtCommentDao tdNtCommentDao;
    @Override
    public RetInfo replay(TdNtCommentInsert tdNtCommentInsert) {
        String logInfo = this.getClass().getName() + ":index:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            tdNtCommentDao.updateTdNtComment(tdNtCommentInsert);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findCommentByCourse(Page page) {
        String logInfo = this.getClass().getName() + ":findCommentByCourse:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String , Object> map = new HashMap<>();
            int currentPage = Integer.valueOf(page.getCurrentPage());
            int pageSize = page.getPageSize();//每页数据
            int totalPage = 0;//总页数
            int totalRecord = tdNtCommentDao.findTdNtCommentByCouIdSize(page);//总条数
            List<TdNtCommentList> tdNtTeacherLists = null;
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    currentPage = 1;
                }
                page.setCurrentPage((currentPage - 1) * pageSize);
                tdNtTeacherLists = tdNtCommentDao.findTdNtCommentByCouIdPage(page);

            }
            map.put("comments", tdNtTeacherLists);
            map.put("totalPage", totalPage);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo findCommentByTeacher(Page page) {
        String logInfo = this.getClass().getName() + ":findCommentByTeacher:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            Map<String , Object> map = new HashMap<>();
            int currentPage = Integer.valueOf(page.getCurrentPage());
            int pageSize = page.getPageSize();//每页数据
            int totalPage = 0;//总页数
            int totalRecord = tdNtCommentDao.findCommentByTeaIdSize(page);//总条数
            List<TdNtCommentList> tdNtTeacherLists = null;
            if (totalRecord > 0) {
                if (totalRecord % pageSize == 0) {
                    totalPage = totalRecord / pageSize;
                } else {
                    totalPage = (totalRecord / pageSize) + 1;
                }
                if (currentPage >= totalPage) {
                    currentPage = totalPage;
                }
                if (currentPage <= 1) {
                    currentPage = 1;
                }
                page.setCurrentPage((currentPage - 1) * pageSize);
                tdNtTeacherLists = tdNtCommentDao.findCommentByTeaIdPage(page);

            }
            map.put("comments", tdNtTeacherLists);
            map.put("totalPage", totalPage);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(map);
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }

    @Override
    public RetInfo updateRecordingPlaySize(int stt_id) {
        String logInfo = this.getClass().getName() + ":updateRecordingPlaySize:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtCommentList tdNtCommentList = tdNtCommentDao.findTdNtCommentBySttId(stt_id);
            Map<String, Object> map = new HashMap<>();
            map.put("stt_id",stt_id);
            map.put("play_size",(tdNtCommentList.getPlay_size()+1));
            tdNtCommentDao.updateCommentRecordPlaySize(map);

            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
